package jp.sourceforge.akjrcp.util;

import com.akjava.lib.standards.process.ProcessMonitor;
import com.akjava.lib.standards.process.ProcessPrinter;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;


/**
 * @deprecated  Use jp.sourceforge.akjrcp.ffmpeg.FFMpegExecuter
 */
public   class FFMpegExecuter {

	private String ffmpegPath;
	private File input;
	private File output;
	private ProcessPrinter printer;
	
	private int videoBitrate;
	private int audioBitrate;
	private int audioSampleRate;
	private int audioChannel;
	private double videoRate;
	
	private String videoSize;
	
	private boolean useAvisynth;
	public boolean isUseAvisynth() {
		return useAvisynth;
	}
	public void setUseAvisynth(boolean useAvisynth) {
		this.useAvisynth = useAvisynth;
	}
	public FFMpegExecuter(String ffmpegPath,ProcessPrinter printer){
		this.ffmpegPath=ffmpegPath;
		this.printer=printer;
	}
	public void execute() throws IOException{
		Runtime runtime=Runtime.getRuntime();
		String commands[]=toCommands();
		
		printer.printOutput(toDebugArray(commands));
		
		Process process=runtime.exec(commands);
		ProcessMonitor monitor=new ProcessMonitor(process,printer);
		monitor.startMonitor();
		
		try {
			process.waitFor();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public String toDebugArray(String[] array){
		String v="";
		for (int i = 0; i < array.length; i++) {
			v+="\""+array[i]+"\""+" ";
		}
		return v+"\n";
	}
	
	public String[] toCommands(){
		ArrayList list=new ArrayList();
		
		list.add(ffmpegPath);
		
		list.add("-i");
		list.add(input.getAbsolutePath());
		
		if(videoBitrate!=0){
			list.add("-b");
			list.add(""+videoBitrate+"k");
			
			list.add("-maxrate");
			list.add(""+videoBitrate+"k");
			
			list.add("-bufsize");
			list.add(""+256+"k");
		}
		
		if(videoRate!=0){
			list.add("-r");
			list.add(""+videoRate);
		}
		
		if(videoSize!=null){
			list.add("-s");
			list.add(videoSize);
		}
		
		if(audioSampleRate!=0){
			list.add("-ar");
			list.add(""+audioSampleRate);
		}
		
		if(audioBitrate!=0){
			list.add("-ab");
			list.add(""+audioBitrate+"k");
		}
		
		if(audioChannel!=0){
			list.add("-ac");
			list.add(""+audioChannel);//critical bug.
		}
		
		list.add(output.getAbsolutePath());
		return (String[])list.toArray(new String[0]);
	}


	public int getAudioBitrate() {
		return audioBitrate;
	}


	public void setAudioBitrate(int audioBitrate) {
		this.audioBitrate = audioBitrate;
	}


	public int getAudioChannel() {
		return audioChannel;
	}


	public void setAudioChannel(int audioChannel) {
		this.audioChannel = audioChannel;
	}


	public int getAudioSampleRate() {
		return audioSampleRate;
	}


	public void setAudioSampleRate(int audioSampleRate) {
		this.audioSampleRate = audioSampleRate;
	}


	public String getFfmpegPath() {
		return ffmpegPath;
	}


	public void setFfmpegPath(String ffmpegPath) {
		this.ffmpegPath = ffmpegPath;
	}


	public File getInput() {
		return input;
	}


	public void setInput(File input) {
		this.input = input;
	}


	public File getOutput() {
		return output;
	}


	public void setOutput(File output) {
		this.output = output;
	}


	public int getVideoBitrate() {
		return videoBitrate;
	}


	public void setVideoBitrate(int videoBitrate) {
		this.videoBitrate = videoBitrate;
	}


	public double getVideoRate() {
		return videoRate;
	}


	public void setVideoRate(double videoRate) {
		this.videoRate = videoRate;
	}


	public String getVideoSize() {
		return videoSize;
	}


	public void setVideoSize(String videoSize) {
		this.videoSize = videoSize;
	}
	
	public int getVideoWidth(){
		if(videoSize!=null){
			int sepa=videoSize.indexOf("x");
			int w=Integer.parseInt(videoSize.substring(0,sepa));
			return w;
		}
		return 0;
	}
	
	public int getVideoHeight(){
		if(videoSize!=null){
			int sepa=videoSize.indexOf("x");
			int h=Integer.parseInt(videoSize.substring(sepa+1));
			return h;
		}
		return 0;
	}

}
